/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Common.CommonUtil;
import DAO.AccountDAO;
import DAO.NotificationDAO;
import hibernate.entity.Account;
import hibernate.entity.Notification;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Tùng
 */
public class AccountController extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        try {
            String action = request.getParameter("action");
            if (action.equals("changePass")) {
                Account getAcc = (Account) session.getAttribute("UPDATEACCOUNTDETAIL");
                if (getAcc != null) {
                    String email = getAcc.getEmailAddress();
                    String oldPassword = request.getParameter("txtChangeOldPassword");
                    String convertPassword = CommonUtil.md5(oldPassword);
                    AccountDAO accDAO = new AccountDAO();
                    Account acc = accDAO.checkAccountByPassword(email, convertPassword);
                    if (acc != null) {
                        String newPasword = request.getParameter("txtChangeNewPassword");
                        String convertNewPassword = CommonUtil.md5(newPasword);
                        int accountID = acc.getAccountID();
                        boolean checkChange = accDAO.changePassword(convertNewPassword, accountID);
                        if (checkChange) {
                            request.setAttribute("CHANGESUCCESS", "Your password changed successfully!!!");
                            session.removeAttribute("CHANGEPASSWORD");
                        } else {
                            request.setAttribute("CHANGEERR", "You can't change your password!!!");
                        }
                        RequestDispatcher rd = request.getRequestDispatcher(Common.Global.userpage);
                        rd.forward(request, response);
                    } else {
                        request.setAttribute("CHANGEFAIL", "Your old password is wrong!!!");
                        RequestDispatcher rd = request.getRequestDispatcher(Common.Global.userpage);
                        rd.forward(request, response);
                    }
                } else if (getAcc == null) {
                    response.sendRedirect(Common.Global.loginpage);
                }
            } else if (action.equals("updateAccountDetail")) {
                int accID = ((Integer) session.getAttribute("LOGINID")).intValue();
                if (accID != 0) {
                    AccountDAO accDAO = new AccountDAO();
                    Account account = accDAO.getAccountById(accID);
                    NotificationDAO noDAO = new NotificationDAO();
                    session.setAttribute("UPDATEACCOUNTDETAIL", account);

                    int no1 = 1;
                    int no2 = 2;
                    Notification noti1 = noDAO.checkNotification(accID, no1);
                    boolean accepted1 = noti1.isAccepted();
                    if (accepted1 == true) {
                        session.setAttribute("ACCEPTED1", accepted1);
                    }
                    Notification noti2 = noDAO.checkNotification(accID, no2);
                    boolean accepted2 = noti2.isAccepted();
                    if (accepted2 == true) {
                        session.setAttribute("ACCEPTED2", accepted2);
                    }
                    response.sendRedirect(Common.Global.userpage);
                } else {
                    response.sendRedirect(Common.Global.homepage);
                }
            } else if (action.equals("updateInformation")) {
                session.removeAttribute("ACCEPTED1");
                session.removeAttribute("ACCEPTED2");
                Account acc = (Account) session.getAttribute("ACCOUNT");
                if (acc != null) {
                    String updateUsername = request.getParameter("txtUpdateUsername");
                    AccountDAO accDAO = new AccountDAO();
                    NotificationDAO noDAO = new NotificationDAO();
                    boolean check = accDAO.updateFullName(updateUsername, acc.getAccountID());
                    if (check) {
                        for (int i = 1; i < 3; i++) {
                            Notification noti = noDAO.checkNotification(acc.getAccountID(), i);
                            if (noti != null) {
                                String notifi = request.getParameter("ckNoti" + i);
                                boolean accepted = true;
                                if (notifi != null) {
                                    accepted = true;
                                } else {
                                    accepted = false;
                                }
                                noDAO.updateNotification(accepted, noti.getNoID());
                            } else {
                                noDAO.insertNotification(acc.getAccountID(), i);
                            }
                        }
                        session.setAttribute("UPDATESUC", "You updated information successfully!!!");
                        response.sendRedirect(Common.Global.homepage);
                    } else {
                        session.setAttribute("UPDATEERR", "You updated information failed!!! Check again please!!!");
                        response.sendRedirect(Common.Global.userpage);
                    }
                } else if (acc == null) {
                    response.sendRedirect(Common.Global.loginpage);
                }
            } else if (action.equals("ChangePassword")) {
                session.setAttribute("CHANGEPASSWORD", out);
                response.sendRedirect(Common.Global.userpage);
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
